
package com.insigma.business.qrcode.soldier._provider.impl;


import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.CommonSmsPoolDTO;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.soldier.entity.CommonSmsPoolDO;
import com.insigma.business.qrcode.soldier.entity.SoldierBasicInfoDO;
import com.insigma.business.qrcode.common.converter.CommonSmsPoolProviderConverter;
import com.insigma.business.qrcode.soldier.util.RequestSetUtils;
import com.insigma.business.qrcode.soldier.request.*;
import com.insigma.business.qrcode.soldier._provider.CommonSmsPoolProvider;
import com.insigma.business.qrcode.soldier.service.CommonSmsPoolService;
import com.insigma.business.qrcode.soldier.service.SoldierBasicInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Slf4j
@Service
@Component("commonSmsPoolProvider")
public class CommonSmsPoolProviderImpl extends BaseProviderImpl implements CommonSmsPoolProvider {

    @Autowired
    private CommonSmsPoolService commonSmsPoolService;

    @Autowired
    private SoldierBasicInfoService soldierBasicInfoService;


    @Override
    public BasePagingResponse<List<CommonSmsPoolDTO>> searchCommonSmsPool(final CommonSmsPoolSearchRequest searchRequest) {

        final BasePagingResponse<List<CommonSmsPoolDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CommonSmsPool的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final CommonSmsPoolBSearcher commonSmsPoolBSearcher = CommonSmsPoolProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<CommonSmsPoolDO> commonSmsPoolDOList = commonSmsPoolService.search(commonSmsPoolBSearcher);
                final List<CommonSmsPoolDTO> commonSmsPoolDTOList = CommonSmsPoolProviderConverter.convertResponseDOList2DTOList(commonSmsPoolDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, commonSmsPoolDTOList, searchRequest,commonSmsPoolBSearcher.getTotalCount());
             }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_SMS_POOL_SEARCH;
            }
        });

        return basePagingResponse;
    }

    @Override
    public BaseResponse<List<CommonSmsPoolDTO>> listByAreaUsed(CommonSmsPoolSearchRequest searchRequest) {


        BizCheckUtil.checkNotNull(searchRequest, "查询分页CommonSmsPool的请求参数不能为空");
        BizCheckUtil.checkNotNull(searchRequest.getAdminAreaCode(), "查询分页CommonSmsPool的请求参数不能为空");
        CommonSmsPoolBSearcher bSearcher = new CommonSmsPoolBSearcher(1, Integer.MAX_VALUE);
        bSearcher.setAdminAreaCode(searchRequest.getAdminAreaCode());
        bSearcher.setUsed(false);
        final List<CommonSmsPoolDO> commonSmsPoolDOList = commonSmsPoolService.search(bSearcher);
        final List<CommonSmsPoolDTO> commonSmsPoolDTOList = CommonSmsPoolProviderConverter.convertResponseDOList2DTOList(commonSmsPoolDOList);
        return ResponseUtils.buildSuccessResponse(commonSmsPoolDTOList);
    }



    @Override
    public BaseResponse<CommonSmsPoolDTO> getCommonSmsPool(final BaseGetRequest baseGetRequest){

        final BaseResponse<CommonSmsPoolDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CommonSmsPool的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final CommonSmsPoolDO commonSmsPoolDO = commonSmsPoolService.get((String)baseGetRequest.getId());
                final CommonSmsPoolDTO remoteObj = CommonSmsPoolProviderConverter.convertResponseDO2DTO(commonSmsPoolDO);
                ResponseUtils.setupSuccessResponse(baseResponse,remoteObj);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_SMS_POOL_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertCommonSmsPool(final CommonSmsPoolInsertRequest insertRequest){

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增CommonSmsPool的请求参数不能为空");
                BizCheckUtil.checkNotBlank(insertRequest.getAdminAreaCode(), "地区码不能为空");
                BizCheckUtil.checkNotBlank(insertRequest.getMessage(), "消息不能为空");

            }
            @Override
            public void executeService() {
                final CommonSmsPoolDO commonSmsPoolDO = CommonSmsPoolProviderConverter.convertRequestIO2DO(insertRequest);
                if (Boolean.TRUE.equals(insertRequest.getAllSend())) {
                    List<String> mobiles = soldierBasicInfoService.listAllMobile(insertRequest.getAdminAreaCode());
                    commonSmsPoolDO.setMobileList(String.join(",", mobiles));
                } else {
                    List<SoldierBasicInfoDO> soldierBasicInfoDOS = soldierBasicInfoService.listByIdCardNoList(insertRequest.getIdCardNoList());
                    Set<String> mobiles = soldierBasicInfoDOS.stream().filter(s -> Strings.isNotBlank(s.getMobile())).map(s -> s.getMobile()).collect(Collectors.toSet());
                    commonSmsPoolDO.setMobileList(String.join(",", mobiles));
                }
                commonSmsPoolDO.setUsed(false);
                commonSmsPoolService.insert(commonSmsPoolDO);
                ResponseUtils.setupSuccessResponse(baseResponse, commonSmsPoolDO.getId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_SMS_POOL_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updateCommonSmsPool(final CommonSmsPoolUpdateRequest updateRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改CommonSmsPool的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(),"操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(),"操作人类型不能为空");
            }
            @Override
            public void executeService() {
                CommonSmsPoolDO commonSmsPoolDO = CommonSmsPoolProviderConverter.convertRequestUO2DO(updateRequest);
                Integer affectRowNum = commonSmsPoolService.update(commonSmsPoolDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_SMS_POOL_UPDATE;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> setAsUsed(CommonSmsPoolUpdateRequest updateRequest) {

        BizCheckUtil.checkNotNull(updateRequest, "修改CommonSmsPool的请求参数不能为空");
        BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
        CommonSmsPoolDO commonSmsPoolDO = commonSmsPoolService.get(updateRequest.getId());
        if (commonSmsPoolDO != null) {
            commonSmsPoolDO.setUsed(true);
            RequestSetUtils.setModifierInfo(commonSmsPoolDO, updateRequest);
            commonSmsPoolService.update(commonSmsPoolDO);
        }
        return ResponseUtils.buildSuccessResponse(0);
    }


    @Override
    public BaseResponse<Integer> deleteCommonSmsPool(final BaseDeleteRequest deleteRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除CommonSmsPool的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(),"操作人类型不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getModifierId(),"操作人不能为空");
            }
            @Override
            public void executeService() {
                final Integer affectRowNum = commonSmsPoolService.delete((String)deleteRequest.getId(),deleteRequest.getModifierType(),(String)deleteRequest.getModifierId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_SMS_POOL_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }
}
